package com.lianxian.xindong.mvp.ui.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.hxb.library.base.BaseActivity;
import com.hxb.library.base.BaseFragment;
import com.hxb.library.base.BasePagerAdapter;
import com.hxb.library.base.MessageEvent;
import com.hxb.library.di.component.AppComponent;
import com.hxb.library.integration.EventBusManager;
import com.hxb.library.utils.HxbUtils;

import com.lianxian.xindong.commonres.weight.NoScrollViewPager;
import com.lianxian.xindong.commonsdk.core.EventBusHub;
import com.lianxian.xindong.commonsdk.core.RouterHub;
import com.lianxian.xindong.commonservice.home.service.HomeMainTableService;
import com.lianxian.xindong.commonservice.me.service.MeMainTableService;
import com.lianxian.xindong.di.component.DaggerMainComponent;
import com.lianxian.xindong.mvp.contract.MainContract;
import com.lianxian.xindong.mvp.presenter.MainPresenter;

import com.lianxian.xindong.R;


import java.util.ArrayList;

import javax.inject.Inject;

import butterknife.BindView;

import static com.hxb.library.utils.Preconditions.checkNotNull;


/**
 * @作者：hexingbo
 * @时间：06/30/2023
 * @描述：
 */
@Route(path = RouterHub.APP_MAINACTIVITY)
public class MainActivity extends BaseActivity<MainPresenter> implements MainContract.View {

    private long mPressedTime;

    @Inject
    Dialog mDialog;

    @BindView(R.id.view_pager)
    NoScrollViewPager mViewPager;
    @BindView(R.id.tab_layout)
    CommonTabLayout mTabLayout;

    @Autowired(name = RouterHub.ME_SERVICE_MEMAINTABLESERVICE)
    MeMainTableService mMeMainTableService;
    @Autowired(name = RouterHub.HOME_SERVICE_HOMEMAINTABLESERVICE)
    HomeMainTableService mHomeMainTableService;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerMainComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_main; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mPresenter.loadView();
    }

    @Override
    public void showLoading() {
        mDialog.show();
    }

    @Override
    public void hideLoading() {
        mDialog.dismiss();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        HxbUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        HxbUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void onBackPressed() {
        if (mTabLayout.getCurrentTab() != 0) {
            setPageSelected(0);
            return;
        }
        //获取第一次按键时间
        long mNowTime = System.currentTimeMillis();
        //比较两次按键时间差
        if ((mNowTime - mPressedTime) > 2000) {
            showMessage("再按一次退出" + HxbUtils.getString(getApplicationContext(), R.string.public_app_name));
            mPressedTime = mNowTime;
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public Activity getContext() {
        return this;
    }

    @Override
    public void setContentViewData(ArrayList<CustomTabEntity> tabEntities, ArrayList<BaseFragment> fragments) {
        mTabLayout.setTabData(tabEntities);
        mTabLayout.setCurrentTab(0);
        mTabLayout.setOnTabSelectListener(mPresenter.getOnTabSelectListener());
        mViewPager.setAdapter(new BasePagerAdapter(getSupportFragmentManager(), fragments));
        mViewPager.setOffscreenPageLimit(fragments.size());
        mViewPager.addOnPageChangeListener(mPresenter.getOnPageChangeListener());
    }

    @Override
    public void setCurrentTab(int position) {
        mTabLayout.setCurrentTab(position);
    }

    @Override
    public void setPageSelected(int position) {
        mViewPager.setCurrentItem(position, false);
    }

    @Override
    public MeMainTableService getMeMainTableService() {
        return mMeMainTableService;
    }

    @Override
    public HomeMainTableService getHomeMainTableService() {
        return mHomeMainTableService;
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.changeBarColor(mViewPager.getCurrentItem());
    }

    @Override
    protected void onPause() {
        super.onPause();
        EventBusManager.getInstance().post(new MessageEvent(EventBusHub.EVENT_SELFIEFRAGMENT_VIDEOPLAYPAUSE));
    }
}
